#pragma once
#include <iostream>
#include <cmath>

/**
 * \class Hitbox
 * \brief A basic square used as hitbox. Can check if two hitboxes collide.
 * \author Xerxes
 * \version 1.0
 * \date 21/02/2013
 */
class Hitbox
{
public:
	Hitbox() : m_x(0), m_y(0), m_w(0), m_h(0){}
	Hitbox(float ww, float hh) : m_x(0), m_y(0), m_w(ww), m_h(hh){}
	Hitbox(float xx, float yy, float ww, float hh) : m_x(xx), m_y(yy), m_w(ww), m_h(hh){}

	/**
	 * \fn bool collide(Hitbox const& hit)
	 * \brief Checks if the box collides with another
	 * \return true if they collide, false otherwise
	 */
	bool collide(Hitbox const& hit) const /*{return ((hit.m_x >= this->m_x && hit.m_x <= this->maxX() 
									|| hit.maxX() >= this->m_x && hit.maxX() <= this->maxX())
									&&
									(hit.m_y >= this->m_y && hit.m_y <= this->maxY() 
									|| hit.maxY() >= this->m_y && hit.maxY() <= this->maxY())
									);}*/
									{return (abs(hit.centerX() - this->centerX()) < (hit.m_w + this->m_w)/2.f
									&&
									(abs(hit.centerY() - this->centerY()) < (hit.m_h + this->m_h)/2.f)
									);}
	
	/**
	 * \fn float x() const
	 * Position of the left border of the box
	 */
	float x() const {return m_x;}
	/**
	 * \fn float y() const
	 * Position of the top border of the box
	 */
	float y() const {return m_y;}
	/**
	 * \fn float width() const
	 * Width of the box
	 */
	float width() const {return m_w;}
	/**
	 * \fn float height() const
	 * Height of the box
	 */
	float height() const {return m_h;}
	/**
	 * \fn float maxX() const
	 * Position of the right border of the box
	 */
	float maxX() const {return m_x + m_w;}
	/**
	 * \fn float maxY() const
	 * Position of the bottom border of the box
	 */
	float maxY() const {return m_y + m_h;}
	/**
	 * \fn float centerX() const
	 * Central X position of the box
	 */
	float centerX() const {return m_x + m_w/2;}
	/**
	 * \fn float centerY() const
	 * Central Y position of the box
	 */
	float centerY() const {return m_y + m_h/2;}


	void setX(float value){m_x = value;}
	void setY(float value){m_y = value;}
	void setPos(float nx, float ny){m_x = nx;m_y=ny;}
	void setWidth(float value){m_w = value;}
	void setHeight(float value){m_h = value;}
	void setSize(float nw, float nh){m_w = nw;m_h=nh;}

	/**
	 * \fn bool operator==(Hitbox& hit) const
	 * \brief Compare two hitboxes.
	 * \return true if they have the exact same position and size.
	 */
	bool operator==(Hitbox const& hit) const {return (m_x == hit.m_x && m_w == hit.m_w && m_y == hit.m_y && m_h == hit.m_h);}

	/**
	* \fn friend std::ostream & operator<<(std::ostream &os, Hitbox const& hit)
	* \brief Simple display for console debug purpose only.
	*/
	friend std::ostream & operator<<(std::ostream &os, Hitbox const& hit){
		return os << "Hit(" << hit.x() << "," << hit.y() << "," << hit.width() << "," << hit.height() << ")";
	}

private:
	float m_x, m_y, m_w, m_h;
};
